var FormWizard = function () {


    return {    	
        //main function to initiate the module
        init: function () {
           

            var form = $('#form');
            var error = $('.alert-error', form);
            var success = $('.alert-success', form);                    

            form.validate({
                doNotHideMessage: true, //this option enables to show the error/success messages on tab switch.
                errorElement: 'span', //default input error message container
                errorClass: 'validate-inline', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                rules: {
                    
                    title: {
                        required: true
                    },
                    count: {
                    	digits:true,
                    	required:true
                    },
                    term: {
                    	digits : true,
                    	required:true
                    },
                    refundAmount:{
                    	number:true,
                    	required:true
                    },
                    amount:{
                    	number:true,
                    	required:true
                    },
                    minInvestAmount:{
                    	number:true,
                    	required:true
                    },
                    maxInvestAmount:{
                    	number:true,
                    	required:true
                    },
                    startTime:{
                    	required:true
                    },
                    endTime:{
                    	required:true
                    },
                    profitType:{
                    	maxlength: 10,
                    	required:true
                    },
                    title:{
                    	maxlength: 100,
                    	required:true
                    },
                    name:{
                    	maxlength: 100,
                    	required:true
                    },
                    acctOpeningBank:{
                    	maxlength: 100,
                    	required:true
                    },
                    cardNumber:{
                    	maxlength: 20,
                    	digits:true,
                    	required:true
                    },
                    accountTitle:{
                    	maxlength: 100,
                    	required:true
                    },
                    province:{
                    	required:true
                    },
                    city:{
                    	required:true
                    },
                    area:{
                    	required:true
                    },
                    address:{
                    	required:true
                    },
                    code:{
                    	required:true
                    }
                                        
                   /*
                    'payment[]': {
                        required: true,
                        minlength: 1
                    }*/
                },

              messages: { // custom messages for radio buttons and checkboxes
                    'payment[]': {
                        required: "Please select at least one option",
                        minlength: jQuery.format("Please select at least one option")
                       
                    },
                    'title':{
                    	required:"标题不能为空",
                    },
                    'minInvestAmount':{
                    	required:"最小投资金额不能少于0",
                    },
                    'maxInvestAmount':{
                    	required:"最大投资金额不能少于0",
                    },
                    'term':{
                    	required:"投资期限不能少于0",
                    	digits:jQuery.format("请输入整数")
                    },
                    'profitType':{
                    	required:"收益方式不能为空",
                    },
                    'refundAmount':{
                    	required:"返现金额不能为空",
                    	number:"请输入正确的数字"
                    },
                    'count':{
                    	required:"数量不能为空",
                    	digits:"请输入整数"
                    },
                    'amount':{
                    	required:"投资金额不能为空",
                    	number:"请输入正确的数字"
                    },
                    'name':{
                    	required:"商户名称不能为空",
                    },
                    'acctOpeningBank':{
                    	required:"开户行不能为空"
                    },
                    'cardNumber':{
                    	required:"卡号不能为空",
                    	digits:"请输入正确的数字"
                    },
                    'accountTitle':{
                    	required:"户名不能为空"
                    },
                    'province':{
                    	required:"省份不能为空"
                    },
                    'city':{
                    	required:"市不能为空"
                    },
                    'area':{
                    	required:"区不能为空"
                    },
                    'address':{
                    	required:"详细地址不能为空"
                    },
                    'code':{
                    	required:"商户编号不能为空"
                    }
            
                    
                },

                errorPlacement: function (error, element) { // render error placement for each input type
                    if (element.attr("name") == "gender") { // for uniform radio buttons, insert the after the given container
                        error.addClass("no-left-padding").insertAfter("#form_gender_error");
                    } else if (element.attr("name") == "payment[]") { // for uniform radio buttons, insert the after the given container
                        error.addClass("no-left-padding").insertAfter("#form_payment_error");
                    } else {
                        error.insertAfter(element); // for other inputs, just perform default behavoir
                    }
                },

                invalidHandler: function (event, validator) { //display error alert on form submit   
                    success.hide();
                    error.show();
                    //App.scrollTo(error, -200);
                },

                highlight: function (element) { // hightlight error inputs
                    $(element)
                        .closest('.help-inline').removeClass('ok'); // display OK icon
                    $(element)
                        .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
                },

                unhighlight: function (element) { // revert the change dony by hightlight
                    $(element)
                        .closest('.control-group').removeClass('error'); // set error class to the control group
                },

                success: function (label) {
                    if (label.attr("for") == "gender" || label.attr("for") == "payment[]") { // for checkboxes and radip buttons, no need to show OK icon
                        label
                            .closest('.control-group').removeClass('error').addClass('success');
                        label.remove(); // remove error label here
                    } else { // display success icon for other inputs
                        label
                            .addClass('valid ok') // mark the current input as valid and display OK icon
                        .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group
                    }
                },

                submitHandler: function (form) {
                    success.show();
                    error.hide();
                    
                                
             
                    var startTime=$("#start_time").val();  
                    var start=new Date(startTime.replace("-", "/").replace("-", "/"));  
                    var endTime=$("#end_time").val();  
                    var end=new Date(endTime.replace("-", "/").replace("-", "/"));  
                    var amount = $("#amount").val();  
                    var minAmount=$("#minInvestAmount").val();
                    var maxAmount=$("#maxInvestAmount").val();
                    var start_time=$("#startTime").val();
                    var end_time=$("#endTime").val();
                    var status=$("#status").val();
                    var conment=$("#useGuide").val();
                    var desc=$("#description").val();
                    if(status == 0 || status == 1 || status == 3){
                    	alertBasic("警告","已审核、已删除、审核失败的产品不允许修改",null,1);
                    	return false;
                    }
                    if(Number(maxAmount) < Number(minAmount)){
                    	alertBasic("警告","最大投资金额不能小于最小投资金额",null,1);
                    	return false;
                    }
                    if(end<start){ 
                    	alertBasic("警告","开始时间不能大于结束时间",null,1);
                    	return false;
                    }
                    if(end_time<start_time){ 
                    	alertBasic("警告","开始时间不能大于结束时间",null,1);
                    	return false;
                    }
                    if(amount <= 0){
                    	alertBasic("警告","金额不能为0",null,1);
                    	return false;	
                    }
                    if(conment.length > 2000 ){
                    	alertBasic("警告","使用说明录入字数不能超过2000字",null,1);
                    	return false;	
                    }
                    if(desc.length > 2000 ){
                    	alertBasic("警告","产品说明录入字数不能超过2000字",null,1);
                    	return false;	
                    }
                    form.submit();
             
                }

            });
           
        }

    };

}();